<?php

namespace app\admin\controller\dim;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

use app\common\model\DimCategory;

/**
 * @ControllerAnnotation(title="dim_category")
 */
class Category extends AdminController
{

    use \app\admin\traits\Curd;

    public static $rule = [
        'title|标题' => 'require',
        'pid' => 'require',
        'type' => 'require'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new DimCategory();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();

            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }

        $cate_type_list = DimCategory::$cate_type_map;
        $this->assignData([
            'cate_type_list' => $cate_type_list,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $this->validate($post, self::$rule);

            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        // 分类列表
        $conditions = array();
        $conditions[] = array('is_del', '=', DimCategory::IS_DELETE_NO);
        $conditions[] = array('type', '=', DimCategory::CATE_TYPE_TASK);
        $cate_tree_list = $this->model->getTree($conditions);

        // 分类类型
        $cate_type_list = DimCategory::$cate_type_map;

        $this->assignData([
            'cate_tree_list' => $cate_tree_list,
            'cate_type_list' => $cate_type_list
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $this->validate($post, self::$rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);

        // 分类列表
        $conditions = array();
        $conditions[] = array('is_del', '=', DimCategory::IS_DELETE_NO);
        $conditions[] = array('type', '=', $row['type']);
        $cate_tree_list = $this->model->getTree($conditions);

        // 分类类型
        $cate_type_list = DimCategory::$cate_type_map;
        $this->assignData([
            'cate_tree_list' => $cate_tree_list,
            'cate_type_list' => $cate_type_list
        ]);
        return $this->fetch();
    }

    public function getTree()
    {

        $type_id = input('type_id', '');

        try{
            // 分类列表
            $conditions = array();
            $conditions[] = array('is_del', '=', DimCategory::IS_DELETE_NO);
            $conditions[] = array('type', '=', $type_id);
            $cate_tree_list = $this->model->getTree($conditions);
            _return(0, '操作成功', $cate_tree_list);
        }catch (\Exception $e){
            _return(-1, '操作失败');
        }
    }
}